﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;

namespace MicroBlogClient
{
    public class BaseWindow : Window
    {
        #region const
        private const int RESIZE_BORDER = 8;
        private const int HIDE_BORDER = 3;
        private const int FLASH_TIME = 500;
        private const int MAGNET_BORDER = 20;
        #endregion

        private HwndSource _HwndSource;

        public BaseWindow()
        {
            this.SourceInitialized += delegate(object sender, EventArgs e)
            {
                this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
                this._HwndSource.AddHook(Win32.WindowProc);
            };
            this.ShowInTaskbar = false;
        }

        #region Mouse Event
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            Win32.POINT p;
            if (!Win32.GetCursorPos(out p))
                return;
            if (this.Left + RESIZE_BORDER < p.x && this.Left + this.ActualWidth - RESIZE_BORDER > p.x && this.Top + RESIZE_BORDER < p.y && this.Top + this.ActualHeight - RESIZE_BORDER > p.y)
            {
                this.DragMove();
            }
            base.OnMouseLeftButtonDown(e);
        }

        protected override void OnDeactivated(EventArgs e)
        {
            base.OnDeactivated(e);
        }


        protected override void OnMouseMove(MouseEventArgs e)
        {
            Win32.POINT p;
            if (!Win32.GetCursorPos(out p))
                return;

            if (this.Left <= p.x && this.Left + RESIZE_BORDER >= p.x
                && this.Top <= p.y && this.Top + RESIZE_BORDER >= p.y)
            {
                this.Cursor = Cursors.SizeNWSE;
                if (e.LeftButton == MouseButtonState.Pressed)
                {
#if DEBUG
                    System.Diagnostics.Debug.Print(string.Format("this.MinHeight::{0},this.Height:{1}", this.MinHeight, this.Height));
                    System.Diagnostics.Debug.Print(string.Format("this.MinWidth::{0},this.Height:{1}", this.MinWidth, this.Width));
#endif
                    if (this.Width >= this.MinWidth && this.Height >= this.MinHeight)
                    {
                        Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61444), IntPtr.Zero);
                    }
                }
            }
            else if (this.Left <= p.x && this.Left + RESIZE_BORDER >= p.x
                && this.Top + this.ActualHeight - RESIZE_BORDER <= p.y && this.Top + this.ActualHeight >= p.y)
            {
                this.Cursor = Cursors.SizeNESW;
                if (e.LeftButton == MouseButtonState.Pressed)
                    if (this.Width >= this.MinWidth && this.Height >= this.MinHeight)
                    {
                        Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61447), IntPtr.Zero);
                    }
            }
            else if (this.Left + this.ActualWidth - RESIZE_BORDER <= p.x && this.Left + this.ActualWidth >= p.x
                && this.Top <= p.y && this.Top + RESIZE_BORDER >= p.y)
            {
                this.Cursor = Cursors.SizeNESW;
                if (e.LeftButton == MouseButtonState.Pressed)
                    if (this.Width >= this.MinWidth && this.Height >= this.MinHeight)
                    {
                        Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61445), IntPtr.Zero);
                    }
            }
            else if (this.Left + this.ActualWidth - RESIZE_BORDER <= p.x && this.Left + this.ActualWidth >= p.x
                && this.Top + this.ActualHeight - RESIZE_BORDER <= p.y && this.Top + this.ActualHeight >= p.y)
            {
                this.Cursor = Cursors.SizeNWSE;
                if (e.LeftButton == MouseButtonState.Pressed)
                    if (this.Width >= this.MinWidth && this.Height >= this.MinHeight)
                    {
                        Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61448), IntPtr.Zero);
                    }
            }
            else if (this.Top <= p.y && this.Top + RESIZE_BORDER >= p.y)
            {
                this.Cursor = Cursors.SizeNS;
                if (e.LeftButton == MouseButtonState.Pressed)
                    if (this.Height >= this.MinHeight)
                    {
                        Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61443), IntPtr.Zero);

                    }
            }
            else if (this.Left <= p.x && this.Left + RESIZE_BORDER >= p.x)
            {
                this.Cursor = Cursors.SizeWE;
                if (e.LeftButton == MouseButtonState.Pressed)
                    if (this.Width >= this.MinWidth)
                    {
                        Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61441), IntPtr.Zero);
                    }
            }
            else if (this.Top + this.ActualHeight - RESIZE_BORDER <= p.y && this.Top + this.ActualHeight >= p.y)
            {
                this.Cursor = Cursors.SizeNS;
                if (e.LeftButton == MouseButtonState.Pressed)
                    if (this.Height >= this.MinHeight)
                    {
                        Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61446), IntPtr.Zero);
                    }
            }
            else if (this.Left + this.ActualWidth - RESIZE_BORDER <= p.x && this.Left + this.ActualWidth >= p.x)
            {
                this.Cursor = Cursors.SizeWE;
                if (e.LeftButton == MouseButtonState.Pressed)
                    if (this.Width >= this.MinWidth)
                    {
                        Win32.SendMessage(_HwndSource.Handle, 0x112, (IntPtr)(61442), IntPtr.Zero);
                    }
            }
            else
            {
                this.Cursor = Cursors.Arrow;
            }
            base.OnMouseMove(e);
        }

        #endregion

    }
}
